A line in 3d will be given by its $x,y,$ and $z$ components, and they might be given as three equation, or as a vector
equation. For example
\[
L:\ \left(x=4t+2,\,y=2t-7,\ z=-3t-3\right)
\]
The same line could be written as a vector this way.
\[
\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)=\left(\begin{array}{c}
2\\
-7\\
-3
\end{array}\right)+t\left(\begin{array}{c}
4\\
2\\
-3
\end{array}\right)
\]
Also, if $P=(2,-7,-3)$ and $\mathbf{u}=(4,2,-3),$ then the line
might be
\[
\mathbf{X}=P+t\cdot\mathbf{u}.
\]
A plane will probably be written as
\[
4x+2y-3z=3.
\]
But could just as well be written as
\[
\left(\begin{array}{c}
4\\
2\\
-3
\end{array}\right)\cdot\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)=\left(\begin{array}{c}
4\\
2\\
-3
\end{array}\right)\cdot\left(\begin{array}{c}
2\\
-7\\
-3
\end{array}\right)
\]
which would probably be shortened to
\[
\mathbf{u}\cdot\mathbf{X}=\mathbf{u}\cdot P
\]
to describe the plane with normal $\mathbf{u}$ which includes point
$P.$ The operation here is a dot product. So,
\[
\left(\begin{array}{c}
4\\
2\\
-3
\end{array}\right)\cdot\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)\text{ means }4x+2y-3z.
\]
Method 1: Perpendicular, point to 3d line.
The essence of method 1 is to find the intersection of the line and
the plane orthogonal to the line where that plane contains the external
point. See the figure in the example below.
We are given both the line equation and the point, $P.$ We will need
to know one point on the line. It can be deduced from the line equation,
or we can input any scalar value $t,$ and calculate a point. Call
it $A.$
Name a point on the line. Call it $A.$ Get its coordinates.
Identify the direction vector of the line. Call it $\mathbf{u}.$
If the line was given as being between points $A$ and $B,$ then
\[
\mathbf{u}=(B-A)=\left(\begin{array}{c}
B_{x}-A_{x}\\
B_{y}-A_{y}\\
B_{z}-A_{z}
\end{array}\right).
\]
Write the line equation. Even if it was previously given.
\begin{equation}
\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)=\left(\begin{array}{c}
A_{x}\\
A_{y}\\
A_{z}
\end{array}\right)+t\cdot\left(\begin{array}{c}
u_{x}\\
u_{y}\\
u_{z}
\end{array}\right).\label{eq:eqn for t}
\tag{"the line equation"}
\end{equation}
Write the plane equation that has normal, $\mathbf{u}$ and passes
through $P.$
\[
\mathbf{X}\cdot\mathbf{u}=\mathbf{u}\cdot P.
\]
That is,
\[
\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)\cdot\left(\begin{array}{c}
u_{x}\\
u_{y}\\
u_{z}
\end{array}\right)=\left(\begin{array}{c}
u_{x}\\
u_{y}\\
u_{z}
\end{array}\right)\cdot\left(\begin{array}{c}
P_{x}\\
P_{y}\\
P_{z}
\end{array}\right).
\]
Find the intersection of the line and plane. That point will be the
place on the line which when connected to point $P$ forms an orthogonal
segment. The length of that segment is the distance from $P$ to the
line.
Substitute the 3 equations from the line equation into the plane equation.
Remember that this is a dot product equation.
\[
\left(\begin{array}{c}
A_{x}+t\cdot u_{x}\\
A_{y}+t\cdot u_{y}\\
A_{z}+t\cdot u_{z}
\end{array}\right)\cdot\left(\begin{array}{c}
u_{x}\\
u_{y}\\
u_{z}
\end{array}\right)=\left(\begin{array}{c}
u_{x}\\
u_{y}\\
u_{z}
\end{array}\right)\cdot\left(\begin{array}{c}
P_{x}\\
P_{y}\\
P_{z}
\end{array}\right)
\]
\[
(A_{x}+t\cdot u_{x})(u_{x})+(A_{y}+t\cdot u_{y})(u_{y})+(A_{z}+t\cdot u_{z})(u_{z})=u_{x}P_{x}+u_{y}P_{y}+u_{z}P_{z}.
\]
Solve for $t.$
Plug the found value for $t$ back into the line equation, eqn. \ref{eq:eqn for t}
and calculate the intersection point. Call it $E.$
Calculate the distance between $P$ and $E$ using the distance equation.
\[
dist=|P-E|=\sqrt{\left(P_{x}-E_{x}\right)^{2}+\left(P_{y}-E_{y}\right)^{2}+\left(P_{z}-E_{z}\right)^{2}}
\]
Example: Short Distance to a line in 3d. Given the line between points $A=(-5,2,3)$
and $B=(-1,4,2),$find the perpendicular distance to the line from
point $P=(1,2,1).$ Answer:
We will show the final figure here and then go through getting the
result.
We need to identify the direction vector of the line.
Since we already have two points, it is just $\mathbf{u}=(B-A)$.
\[
\mathbf{u}=\left(\begin{array}{c}
-1\\
4\\
2
\end{array}\right)-\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)=\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)
\]
Now write the equation of the line going through points $A$ and $B.$
We will use point $A$ as the anchor, although either one will work.
\begin{equation}
\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)=\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+t\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\label{eq:theLine}\tag{the Line}
\end{equation}
Next we can write the equation of the plane with normal $\mathbf{u}$
and which includes point $P.$
\[
\mathbf{X\cdot u}=\mathbf{u}\cdot P
\]
\[
\left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)=\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\cdot\left(\begin{array}{c}
1\\
2\\
1
\end{array}\right)=4+4-1=7
\]
Since we need to use this equation, we will multiply it out.
\[
4x+2y-z=7 \label{eq:thePlane}\tag{the Plane}
\]
From \ref{eq:theLine}, we will insert the algebra
expressions for $x,y,$ and $z,$ into \ref{eq:thePlane} equation.
\[
4(4t-5)+2(2t+2)-1(-t+3)=7
\]
and solve for $t.$
\[
t=\frac{26}{21}
\]
Next, substitute the value for $t$ back into the line equation to
compute $(x,y,z).$
\[
\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+\left(\frac{26}{21}\right)\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)=\left(\begin{array}{c}
-1/21\\
94/21\\
37/21
\end{array}\right)=E
\]
We have identified the intersection point on the line which forms
a perpendicular with $P.$ Although we could go directly to the distance
formula, we will instead first write the direction vector from $P$
to $E.$
\[
(E-P)=\left(\begin{array}{c}
-22/21\\
52/21\\
16/21
\end{array}\right)
\]
and the distance is the modulus of that vector.
\[
\left|E-P\right|=\frac{2}{21}\sqrt{861}\approx2.79
\]
Method 2: Perpendicular, Point to Line
The essence of method 2 is to write an expression, $\mathbf{v,}$
for the vector from the external point to the line, $\ell$. Then
notice that said vector will dot product with the line direction vector
to yield zero. Solve that equation for $t,$and then put the $t$
value into equation $\mathbf{v}$ to get the orthogonal direction
vector. Alternatively, put the $t$ value into the line, $\ell,$
to get the intersection point.
Given the line between points $A=(-5,2,3)$ and $B=(-1,4,2),$find
the perpendicular distance to the line from point $P=(1,2,1).$
Let $n$ be the direction vector of the line. So
\[
n=(B-A)=\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)
\]
and the line equation is
\[
\ell:\ \left(\begin{array}{c}
x\\
y\\
z
\end{array}\right)=\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+t\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)
\]
A vector $\mathbf{v},$ from point $P$ to the line would be
\[
\begin{aligned}\mathbf{v} & =\left(A+t\cdot n\right)-P\\
& =\left[\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+t\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\right]-\left(\begin{array}{c}
1\\
2\\
1
\end{array}\right)
\end{aligned}
\]
which can be dot product with $n$ to equal zero. Since the dot product
of orthogonal vectors is zero.
\[
\left(\left[\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+t\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\right]-\left(\begin{array}{c}
1\\
2\\
1
\end{array}\right)\right)\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)=0
\]
\[
\begin{aligned}\left(-5+4t-1\right)4+\left(2+2t-2\right)2+\left(3-t-1\right)\left(-1\right) & =0\\
\left(-20+16t-4\right)+\left(4+4t-4\right)+\left(-3+t+1\right) & =0\\
-24-2+21t & =0\\
-26+21t & =0\\
t & =\frac{26}{21}
\end{aligned}
\]
Solve for $t:$ $t=26/21.$
Put this $t$ value into $\mathbf{v}.$
\[
\mathbf{v}=\left[\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+\left(\frac{26}{21}\right)\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\right]-\left(\begin{array}{c}
1\\
2\\
1
\end{array}\right)=\left(\begin{array}{c}
-22/21\\
52/21\\
16/21
\end{array}\right)\Leftarrow\text{Perpendicular Vector}
\]
Or,
\[
\text{Intersection Point}:\ \left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+\left(\frac{26}{21}\right)\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)=\left(\begin{array}{c}
-1/21\\
94/21\\
37/21
\end{array}\right)
\]
Method 3: Perpendicular, Point to Line
The essence of method 3 is vector projection. We will build a right
triangle in the plane of three points. Two of the points, $A$ and
$E$ will be on the line and the third point, $P$ will be external
to the line. Point $A$ on the line is arbitrary. Point $P$ is the
external point from which we will try to draw a perpendicular, and
point $E$ is the intersection of the line and the perpendicular from
$A.$
Point $E$:
\[
E=A+\frac{\left\Vert \mathbf{p}\cdot\mathbf{n}\right\Vert }{\mathbf{n\cdot n}}\cdot\mathbf{n}
\]
Distance $d$ can be computed with two options:
\[
d=\frac{\mathbf{\left\Vert p\times n\right\Vert }}{|\mathbf{n}|}
\]
or
\[
d=|E-P|.
\]
Example:
Given the line between points $A=(-5,2,3)$ and $B=(-1,4,2),$find
the perpendicular distance to the line from point $P=(1,2,1).$
Answer: The only work here is to construct the various pieces.
\[
\mathbf{p}=P-A=\left(\begin{array}{c}
1\\
2\\
1
\end{array}\right)-\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)=\left(\begin{array}{c}
6\\
0\\
-2
\end{array}\right)
\]
\[
\mathbf{n=}B-A=\left(\begin{array}{c}
-1\\
4\\
2
\end{array}\right)-\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)=\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)
\]
\[
E=A+\frac{\mathbf{p}\cdot\mathbf{n}}{\mathbf{n\cdot n}}\cdot\mathbf{n}=\left(\begin{array}{c}
-5\\
2\\
3
\end{array}\right)+\frac{\left\Vert \left(\begin{array}{c}
6\\
0\\
-2
\end{array}\right)\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\right\Vert }{\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)}\cdot\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)=\left(\begin{array}{c}
-1/21\\
94/21\\
37/21
\end{array}\right)
\]
\[
\text{distance}=\mathbf{\frac{\left\Vert p\times n\right\Vert }{\left\Vert n\right\Vert }}=\frac{\left\Vert \left(\begin{array}{c}
6\\
0\\
-2
\end{array}\right)\times\left(\begin{array}{c}
4\\
2\\
-1
\end{array}\right)\right\Vert }{\sqrt{4^{2}+2^{2}+(-1)^{2}}}=\frac{\left\Vert \left(\begin{array}{c}
4\\
-2\\
12
\end{array}\right)\right\Vert }{\sqrt{21}}=\frac{2}{21}\cdot\sqrt{861}
\]